package cn.homework.web.servlet;

import cn.homework.domain.Product;
import cn.homework.service.ProductService;
import cn.homework.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author ACT
 */
@WebServlet("/productServlet")
public class ProductServlet extends HttpServlet {
    private ProductService service = new ProductServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String demand = request.getParameter("demand");
        String src = request.getParameter("src");
//        System.out.println("demand" + demand);
//        System.out.println("src" + src);
        if (src == null || "".equals(src) || "home".equals(src)) {
            if (demand == null || "".equals(demand)) {
                productDiscountedShow(request, response);
                productHotShow(request, response);
                productCommonShow(request, response);
                response.sendRedirect(request.getContextPath() + "/CommodityHome.jsp");
            } else if ("query".equals(demand)) {
                productCommonShow(request, response);
                productDiscountedShow(request, response);
                productQueryShow(request, response);
                response.sendRedirect(request.getContextPath() + "/CommodityHome.jsp");
            } else if ("queryKitchen".equals(demand)) {
                productQueryKitchen(request, response);
                productDiscountedShow(request, response);
                productHotShow(request, response);
                response.sendRedirect(request.getContextPath() + "/CommodityHome.jsp");
            } else if ("queryWashing".equals(demand)) {
                productQueryWashing(request, response);
                productDiscountedShow(request, response);
                productHotShow(request, response);
                response.sendRedirect(request.getContextPath() + "/CommodityHome.jsp");
            } else if ("queryComputer".equals(demand)) {
                productQueryComputer(request, response);
                productDiscountedShow(request, response);
                productHotShow(request, response);
                response.sendRedirect(request.getContextPath() + "/CommodityHome.jsp");
            } else if ("queryTV".equals(demand)) {
                productQueryTV(request, response);
                productDiscountedShow(request, response);
                productHotShow(request, response);
                response.sendRedirect(request.getContextPath() + "/CommodityHome.jsp");
            }
        } else if ("login".equals(src)) {
            if (demand == null || "".equals(demand)) {
                productCommonShow(request, response);
                productDiscountedShow(request, response);
                productHotShow(request, response);
                response.sendRedirect(request.getContextPath() + "/CommodityHomeLogin.jsp");
            } else if ("query".equals(demand)) {
                productCommonShow(request, response);
                productDiscountedShow(request, response);
                productQueryShow(request, response);
                response.sendRedirect(request.getContextPath() + "/CommodityHomeLogin.jsp");
            } else if ("queryKitchen".equals(demand)) {
                productQueryKitchen(request, response);
                productDiscountedShow(request, response);
                productHotShow(request, response);
                response.sendRedirect(request.getContextPath() + "/CommodityHomeLogin.jsp");
            } else if ("queryWashing".equals(demand)) {
                productQueryWashing(request, response);
                productDiscountedShow(request, response);
                productHotShow(request, response);
                response.sendRedirect(request.getContextPath() + "/CommodityHomeLogin.jsp");
            } else if ("queryComputer".equals(demand)) {
                productQueryComputer(request, response);
                productDiscountedShow(request, response);
                productHotShow(request, response);
                response.sendRedirect(request.getContextPath() + "/CommodityHomeLogin.jsp");
            } else if ("queryTV".equals(demand)) {
                productQueryTV(request, response);
                productDiscountedShow(request, response);
                productHotShow(request, response);
                response.sendRedirect(request.getContextPath() + "/CommodityHomeLogin.jsp");
            }
        } else if ("show".equals(src)) {
            if (demand == null || "".equals(demand) ||"all".equals(demand)) {
                productAllShow(request, response);
                response.sendRedirect(request.getContextPath() + "/CommodityProductShow.jsp");
            } else if ("queryKitchen".equals(demand)) {
                productAllKitchenShow(request, response);
                response.sendRedirect(request.getContextPath() + "/CommodityProductShow.jsp");
            } else if ("queryWashing".equals(demand)) {
                productAllWashingShow(request, response);
                response.sendRedirect(request.getContextPath() + "/CommodityProductShow.jsp");
            } else if ("queryComputer".equals(demand)) {
                productAllComputerShow(request, response);
                response.sendRedirect(request.getContextPath() + "/CommodityProductShow.jsp");
            } else if ("queryTV".equals(demand)) {
                productAllTVShow(request, response);
                response.sendRedirect(request.getContextPath() + "/CommodityProductShow.jsp");
            }
        } else if ("detail".equals(src) && "detail".equals(demand)) {
            productDetailById(request, response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    /**
     * 新品上架商品展示
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void productCommonShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用service查询全部商品封装成list集合
        List<Product> productCommonList = service.findProductCommon();
        //将商品存入session
        HttpSession session = request.getSession();
        session.setAttribute("productCommonList", productCommonList);
    }

    /**
     * 特价商品显示
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void productDiscountedShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Product> productDiscountedList = service.findProductDiscounted();
        HttpSession session = request.getSession();
        session.setAttribute("productDiscountedList", productDiscountedList);
    }

    /**
     * 热销产品展示
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void productHotShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Product> productHotList = service.findProductHot();
        HttpSession session = request.getSession();
        session.setAttribute("productHotList", productHotList);
    }

    /**
     * 根据查询结果显示热销产品
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void productQueryShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> map = request.getParameterMap();
        List<Product> productHotList = service.findProductQuery(map);
        HttpSession session = request.getSession();
        session.setAttribute("productHotList", productHotList);
    }

    /**
     * 查询厨卫系列展示
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void productQueryKitchen(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Product> productCommonList = service.findProductByKitchen();
        HttpSession session = request.getSession();
        session.setAttribute("productCommonList", productCommonList);
    }

    /**
     * 查询洗衣机系列展示
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void productQueryWashing(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Product> productCommonList = service.findProductByWashing();
        HttpSession session = request.getSession();
        session.setAttribute("productCommonList", productCommonList);
    }

    /**
     * 查询电脑系列展示
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void productQueryComputer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Product> productCommonList = service.findProductByComputer();
        HttpSession session = request.getSession();
        session.setAttribute("productCommonList", productCommonList);
    }

    /**
     * 查询电视机系列展示
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void productQueryTV(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Product> productCommonList = service.findProductByTV();
        HttpSession session = request.getSession();
        session.setAttribute("productCommonList", productCommonList);
    }

    /**
     * 查询所有商品展示
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void productAllShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Product> productAllList = service.findProductAllShow();
        HttpSession session = request.getSession();
        session.setAttribute("productAllList", productAllList);
    }

    /**
     * 查询所有厨卫商品展示
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void productAllKitchenShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Product> productAllList = service.findProductAllKitchenShow();
        HttpSession session = request.getSession();
        session.setAttribute("productAllList", productAllList);
    }

    /**
     * 查询所有洗衣机商品展示
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void productAllWashingShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Product> productAllList = service.findProductAllWashingShow();
        HttpSession session = request.getSession();
        session.setAttribute("productAllList", productAllList);
    }

    /**
     * 查询所有电脑商品展示
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void productAllComputerShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Product> productAllList = service.findProductAllComputerShow();
        HttpSession session = request.getSession();
        session.setAttribute("productAllList", productAllList);
    }

    /**
     * 查询所有电视商品展示
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void productAllTVShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Product> productAllList = service.findProductAllTVShow();
        HttpSession session = request.getSession();
        session.setAttribute("productAllList", productAllList);
    }

    /**
     * 根据商品id查询商品详情
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void productDetailById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Product productDetail = service.findProductDetailById(id);
        HttpSession session = request.getSession();
        session.setAttribute("productDetail", productDetail);
        response.sendRedirect(request.getContextPath() + "/CommodityDetails.jsp");
    }
}
